/**
 * 数据操作服务
 */
export default class LiveService {
    constructor() {

    }

    /**
     * 获取列表
     * @param {*} 
     */
    async getLiveList(token, id, label) {
        var url = ''
        if (id == undefined) {
            url = `${API_PATH}/api/v1/live`
        } else {
            url = `${API_PATH}/api/v1/live/${id}`
        }
        if (label != undefined) {
            url = `${API_PATH}/api/v1/live?label=${label}`
        }
        return new Promise(function(resolve, reject) {
            $.ajax({
                type: "GET",
                url: url,
                dataType: "json",
                data: { a: 1 },
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", token);
                },
                success: function(result, xhr) {
                    resolve(result);
                },
                error: function() {
                    resolve({
                        code: 500,
                        message: "网络繁忙，请稍后再试"
                    });
                },
            });
        });
    }


    /**
     * 获取列表
     * @param {*} 
     */
    async getLiveListByLabel(token, id, label,options) {
        var url = ''
        if (id == undefined) {
            url = `${API_PATH}/api/v1/live`
        } else {
            url = `${API_PATH}/api/v1/live/${id}`
        }
        if (label != undefined) {
            url = `${API_PATH}/api/v1/live?label=${label}`
        }
        return new Promise(function(resolve, reject) {
            $.ajax({
                type: "GET",
                url: url,
                dataType: "json",
                data: {  start: options.start, length: options.length },
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", token);
                },
                success: function(result, xhr) {
                    resolve(result);
                },
                error: function() {
                    resolve({
                        code: 500,
                        message: "网络繁忙，请稍后再试"
                    });
                },
            });
        });
    }



    async getLiveAddress(token, id) {
        return new Promise(function(resolve, reject) {
            $.ajax({
                type: "GET",
                url: `${API_PATH}/api/v1/LiveAddress/${id}`,
                dataType: "json",
                data: { a: 1 },
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", token);
                },
                success: function(result) {
                    resolve(result);
                },
                error: function() {
                    resolve({
                        code: 500,
                        message: "网络繁忙，请稍后再试"
                    });
                }
            });
        });
    }

    //订阅
    async getQR(token) {
        return new Promise(function(resolve, reject) {
            $.ajax({
                type: "GET",
                url: `${API_PATH}/api/v1/SubscripQRCode`,
                dataType: "json",
                data: { a: 1 },
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", token);
                },
                success: function(result) {
                    resolve(result);
                },
                error: function() {
                    resolve({
                        code: 500,
                        message: "网络繁忙，请稍后再试"
                    });
                }
            });
        });
    }

    //邀请
    async getQR2(token, id) {
        return new Promise(function(resolve, reject) {
            $.ajax({
                type: "GET",
                url: `${API_PATH}/api/v1/ShareQRCode`,
                dataType: "json",
                data: { LiveId: id },
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", token);
                },
                success: function(result) {
                    resolve(result);
                },
                error: function() {
                    resolve({
                        code: 500,
                        message: "网络繁忙，请稍后再试"
                    });
                }
            });
        });
    }

    //获取服务器时间
    async getTime(token) {
        return new Promise(function(resolve, reject) {
            $.ajax({
                type: "GET",
                url: `${API_PATH}/api/v1/time`,
                dataType: "json",
                data: { a: 1 },
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", token);
                },
                success: function(result) {
                    resolve(result);
                },
                error: function() {
                    resolve({
                        code: 500,
                        message: "网络繁忙，请稍后再试"
                    });
                }
            });
        });
    }


    //聊天记录
    async getChatMsg(token, id, start, length) {
        return new Promise(function(resolve, reject) {
            $.ajax({
                type: "GET",
                url: `${API_PATH}/api/v1/ChatHistory/${id}?start=${start}&length=${length}`,
                dataType: "json",
                data: { a: 1 },
                beforeSend: function(request) {
                    request.setRequestHeader("Authorization", token);
                },
                success: function(result) {
                    resolve(result);
                },
                error: function() {
                    resolve({
                        code: 500,
                        message: "网络繁忙，请稍后再试"
                    });
                }
            });
        });
    }

        //聊天记录
        async getChatMsg2(token, id, start, length) {
            return new Promise(function(resolve, reject) {
                $.ajax({
                    type: "GET",
                    url: `${API_PATH}/api/v1/ChatHistory/${id}?start=1&length=${length}`,
                    dataType: "json",
                    data: { admin: true },
                    beforeSend: function(request) {
                        request.setRequestHeader("Authorization", token);
                    },
                    success: function(result) {
                        resolve(result);
                    },
                    error: function() {
                        resolve({
                            code: 500,
                            message: "网络繁忙，请稍后再试"
                        });
                    }
                });
            });
        }
    


}